package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.result.SysDeptTreeResult;
import org.feng.condition.Condition;
import org.feng.utils.TreeUtils;
import org.feng.annotation.AccessLog;
import org.feng.base.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.feng.entity.SysDept;
import org.feng.service.ISysDeptService;
import org.feng.base.controller.BaseController;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.Arrays;

/**
 * 部门表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/system/sysDept")
@Tag(name = "部门")
public class SysDeptController extends BaseController {

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 分页列表
     *
     * @param sysDept 查询体
     * @return 返回体
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysDept>> pageList(SysDept sysDept) {
        return Result.success(sysDeptService.page(Condition.getPage(), Wrappers.lambdaQuery(sysDept).orderByAsc(SysDept::getSort)));
    }

    /**
     * 树列表
     *
     * @param sysDept 查询体
     * @return 返回体
     */
    @GetMapping("/treeList")
    @Operation(summary = "树列表")
    public Result<List<SysDeptTreeResult>> treeList(SysDept sysDept) {
        List<SysDept> list = sysDeptService.list(Wrappers.lambdaQuery(sysDept).orderByAsc(SysDept::getSort));
        List<SysDeptTreeResult> treeResultList = toBeanList(list, SysDeptTreeResult.class);
        List<SysDeptTreeResult> sysDeptTreeResults = TreeUtils.treeOut(treeResultList, SysDeptTreeResult::getId, SysDeptTreeResult::getParentId, SysDeptTreeResult::getChildren);
        return Result.success(sysDeptTreeResults);
    }

    /**
     * 列表
     *
     * @param sysDept 查询体
     * @return 返回体
     */
    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysDept>> list(SysDept sysDept) {
        return Result.success(sysDeptService.list(Wrappers.lambdaQuery(sysDept).orderByAsc(SysDept::getSort)));
    }

    /**
     * 保存
     *
     * @param sysDept 保存体
     * @return 返回体
     */
    @PostMapping("/save")
    @AccessLog("新增部门表")
    @Operation(summary = "新增")
    public Result<Boolean> save(@RequestBody SysDept sysDept) {
        return Result.status(sysDeptService.save(sysDept));
    }

    /**
     * 更新
     *
     * @param sysDept 更新体
     * @return 返回体
     */
    @PostMapping("/update")
    @AccessLog("更新部门表")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysDept sysDept) {
        return Result.status(sysDeptService.updateById(sysDept));
    }

    /**
     * 删除
     *
     * @param ids 逗号分割的id
     * @return 返回体
     */
    @GetMapping("/remove")
    @AccessLog("删除部门表")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysDeptService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
